package com.foodplus.www.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.login.CodeBean;
import com.foodplus.www.bean.login.LoginBean;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.LoginPresenter;
import com.foodplus.www.view.impl.LoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends Activity implements LoginView {


    @BindView(R.id.login_back)
    ImageView loginBack;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.btn_sure)
    Button btnSure;
    @BindView(R.id.tv_third_login)
    TextView tvThirdLogin;
    @BindView(R.id.login_qq)
    ImageView loginQq;
    @BindView(R.id.login_weiXin)
    ImageView loginWeiXin;
    @BindView(R.id.login_weiBo)
    ImageView loginWeiBo;
    @BindView(R.id.activity_login)
    RelativeLayout activityLogin;


    LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        presenter = new LoginPresenter(this);
    }

    @OnClick({R.id.login_back, R.id.tv_get_code, R.id.btn_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_back://返回
                finish();
                break;
            case R.id.tv_get_code://获取验证码
                if (checkPhone()) {
                    presenter.SendCode(ParamManager.getInstance().getSendCodeParams(etPhone.getText().toString()));
                }
                break;
            case R.id.btn_sure://提交
                if (checkPhone() && checkCode()) {
                    presenter.Login(ParamManager.getInstance().getLoginParams(etCode.getText().toString(), etPhone.getText().toString()));
                }
                break;
        }
    }

    private boolean checkPhone() {
        boolean flag = false;

        Editable text = etPhone.getText();
        if (text == null || text.length() == 0) {
            Toast.makeText(this, "电话号码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (text.length() == 11) {
            try {
                long phoneNum = Long.parseLong(String.valueOf(text));
                if (phoneNum >= 10000000000l && phoneNum <= 20000000000l) {
                    flag = true;
                }
            } catch (Exception e) {
                Toast.makeText(this, "格式错误", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "格式错误", Toast.LENGTH_SHORT).show();
        }
        return flag;

    }

    private boolean checkCode() {
        boolean flag = false;

        Editable text = etCode.getText();
        if (text == null || text.length() == 0 || text.equals("")) {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (text.length() == 6) {
            try {
                long phoneNum = Long.parseLong(String.valueOf(text));
                if (phoneNum >= 000000 && phoneNum <= 999999) {
                    flag = true;
                }
            } catch (Exception e) {
                Toast.makeText(this, "格式错误", Toast.LENGTH_SHORT).show();
            }
        }
        return flag;
    }


    @Override
    public void onCodeSend(CodeBean codeBean) {
        if (codeBean != null && codeBean.getCode() == 200 && codeBean.getDatas().getError().equals("1")) {
            Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "发送失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onLoginResult(LoginBean loginBean) {
        if (loginBean != null && loginBean.getCode() == 200) {
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

            LoginPresenter.saveUserState(loginBean);

            this.finish();
        } else {
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onSuccess(CodeBean codeBean) {

    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }


}
